<template>
  <div class="login-container">
    <a-card title="登录" style="width: 400px">
      <a-form
        :model="formState"
        name="login"
        @finish="onFinish"
        @finishFailed="onFinishFailed"
      >
        <a-form-item
          label="用户名"
          name="username"
          :rules="[{ required: true, message: '请输入用户名!' }]"
        >
          <a-input v-model:value="formState.username" />
        </a-form-item>

        <a-form-item
          label="密 码"
          name="password"
          :rules="[{ required: true, message: '请输入密码!' }]"
        >
          <a-input-password v-model:value="formState.password" />
        </a-form-item>

        <a-form-item>
          <a-button type="primary" html-type="submit" block>
            登录
          </a-button>
        </a-form-item>
      </a-form>
    </a-card>
  </div>
</template>

<script setup>
import { reactive } from 'vue';
import { useRouter } from 'vue-router';
import { message } from 'ant-design-vue';
import { useUser } from './useUser';

const router = useRouter();
const { setCurrentUser } = useUser();

const formState = reactive({
  username: '',
  password: '',
});

const onFinish = (values) => {

  const isAdmin = values.username === 'admin';
  const userRole = isAdmin ? 'admin' : 'user';
  
  setCurrentUser({
    username: values.username,
    role: userRole
  });
  
  message.success(`登录成功！权限：${isAdmin ? '管理员' : '普通用户'}`);
  router.push('/dashboard');
};

const onFinishFailed = (errorInfo) => {
  console.log('Failed:', errorInfo);
};
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
</style>